<?php

header("Content-type:application/json; charset=UTF-8");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);

require('../service/configure_service.php');
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

$wsname = $_REQUEST['wsname'];
$tokeyKey = $_REQUEST['token'];

class category_ws
{

    public function getAllConfigure()
    {
        $configure_service = new configure_service();
        $lists = $configure_service->findAll();
        return apputil::getJsonDataResultUtil($lists, 'configures');
    }

    public function getCategoryByName($name)
    {
        $configure_service = new configure_service();
        $lists = $configure_service->findConfigureByName($name);
        return apputil::getJsonDataResultUtil($lists, 'configures');
    }

    public function createConfigure($configureObject)
    {
        $configure_service = new configure_service();
        $flag = $configure_service->create($configureObject);
        return apputil::getJsonDataResultUtil($flag, 'success');
    }

    public function updateConfigureByName($configureObject)
    {
        $configure_service = new configure_service();
        $flag = $configure_service->update($configureObject);
        return apputil::getJsonDataResultUtil($flag, 'success');
    }

    public function deleteConfigure($configureObject)
    {
        $configure_service = new configure_service();
        $flag = $configure_service->delete($configureObject);
        return apputil::getJsonDataResultUtil($flag, 'success');
    }

    public function isValidateToken($tokeyKey)
    {
        $configure_service = new configure_service();
        return $configure_service->isValidateTokenKey($tokeyKey);
    }

}

//init class
$category_ws = new category_ws();

if ($category_ws->isValidateToken($tokeyKey)) {
    if ($wsname == 'getallconfigures') {
        print_r($category_ws->getAllConfigure());
    } else if ($wsname == 'getconfigurebyname') {
        print_r($category_ws->getCategoryByName($_REQUEST['name']));
    } else if ($wsname == 'createconfigure' || $wsname == 'updateconfigure') {

        $configure = new Configure();

        $configure->setName(isset($_REQUEST['name']) ? $_REQUEST['name'] : null);
        $configure->setContent(isset($_REQUEST['content']) ? $_REQUEST['content'] : null);

        if ($wsname == 'createconfigure') {

            print_r($category_ws->createConfigure($configure));

        } else if ($wsname == 'updateconfigure') {

            $configure->setName(isset($_REQUEST['name']) ? $_REQUEST['name'] : null);
            print_r($category_ws->updateConfigureByName($configure));

        }

    } else if ($wsname == 'deleteconfigure') {
        $configure = new Configure();
        $configure->setName(isset($_REQUEST['name']) ? $_REQUEST['name'] : null);
        print_r($category_ws->deleteConfigure($configure));
    } else {
        echo 'web service error !!';
    }
} else {
    echo 'Your token is incorrect !!';
}
?>